<template>
  <!-- 1代表点赞 -->
  <van-icon
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    :color="value === 1 ? '#e5645f' : ''"
     @click="onCollect"
  />
</template>

<script>
import { addLike, deleteLike } from "@/api/article";
export default {
  data(){
    return{
    }
  },
  props: {
    value: {
      type: Number,
      required: true,
    },
    articleId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  methods:{
   async onCollect(){
      try {
        // 已点赞情况
        let status = -1
        if(this.value === 1){
          let res = await deleteLike(this.articleId)
          console.log(res);
          this.$toast('取消点赞!')
        }else{
          let res = await addLike(this.articleId)
          console.log(res);
          this.$toast.success('成功点赞!')
          status = 1
        }
        this.$emit('input',status)
      } catch (error) {
        this.$toast.fail('操作失败!')
      }
    }
  }
};
</script>

<style>
</style>